import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_application_1/components/barrage/barrage.dart';
import 'package:flutter_application_1/components/fab/fab.dart';
import 'package:flutter_application_1/components/fab/fab_menu.dart';

class OverlayTest extends StatefulWidget {
  @override
  _OverlayTestState createState() => _OverlayTestState();
}

class _OverlayTestState extends State<OverlayTest> {
  bool _isCollect = false;

  ScrollController _controller = ScrollController();

  _showSnackBar(String val) {
    if (val.isNotEmpty) {
      ScaffoldMessenger.of(context).hideCurrentSnackBar();
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(val),
        ),
      );
    }
  }

  // 发送随机弹幕
  _emitBarrage() {
    switch (Random().nextInt(4)) {
      case 0:
        Barrage.emit(
          context: context,
          child: Text('发射弹幕'),
        );
        break;
      case 1:
        Barrage.emit(
          context: context,
          child: Text(
            '超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕超长的弹幕',
            style: TextStyle(
              fontSize: 14,
              color: Colors.pink,
              decoration: TextDecoration.none,
            ),
          ),
        );
        break;
      case 2:
        Barrage.emit(
          context: context,
          child: FlutterLogo(),
        );
        break;
      case 3:
        Barrage.emit(
          context: context,
          child: FlutterLogo(size: 60),
        );
        break;
      default:
    }
  }

  @override
  void dispose() {
    super.dispose();
    Barrage.remove();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Overlay'),
        centerTitle: true,
      ),
      body: FabMenu(
        color: Colors.indigoAccent,
        bottom: 100,
        // 菜单列表
        list: [
          FabMenuItem(
            icon: Icons.share,
            text: '分享',
            onTap: () {
              _showSnackBar('点击分享');
            },
          ),
          FabMenuItem(
            icon: _isCollect ? Icons.favorite : Icons.favorite_border,
            text: _isCollect ? '已收藏' : '收藏',
            color: _isCollect ? Colors.red : Colors.grey,
            onTap: () {
              setState(() {
                _isCollect = !_isCollect;
              });
            },
          ),
          FabMenuItem(
            icon: Icons.local_fire_department,
            text: '发送弹幕',
            onTap: () {
              _emitBarrage();
            },
          ),
          FabMenuItem(
            icon: Icons.arrow_upward,
            onTap: () {
              _showSnackBar('返回顶部');
              _controller.animateTo(
                0,
                duration: Duration(milliseconds: 300),
                curve: Curves.easeIn,
              );
            },
          ),
        ],
        child: Fab(
          onTap: _emitBarrage,
          child: ListView.builder(
            controller: _controller,
            itemBuilder: (BuildContext context, int index) {
              return Text('右边按钮_可以拖动_点击发射弹幕_$index');
            },
            itemCount: 100,
          ),
        ),
      ),
    );
  }
}
